﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Threading;
using System.IO;

public partial class EditProduct : System.Web.UI.Page
{
    productClassBll pcBll = new productClassBll();
    productBll pBll = new productBll();
    product p = new product();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            if (Request.QueryString["productId"] != null)
            {   
                ViewState["dt"] = pcBll.GetDataTable();
                int Pid = Convert.ToInt32(Request.QueryString["productId"]);
                FillHtmlControl(Pid);
            }
            if (Request.QueryString["state"] != null)
            {
                string state = Request.QueryString["state"].ToString();
                switch (state.Trim())
                {
                    case "update":
                        EditPro();
                        break;                    
                    default:
                        break;
                }                
            }
            
        }
    }
    #region 给HTML控件赋值
    public void FillHtmlControl(int pid)
    {
        p = pBll.GetModel(pid);
        this.m_nameAjax.Value = p.productName.ToString();       
        this.keywordAjax.Value = p.ptag.ToString();
        this.proType.Value = p.Productmodel;
        this.m_hitAjax.Value = p.Views.ToString();
        this.psort.Value = p.pSort.ToString();
        ViewState["cid"]= p.pClassId;
        this.FCKeditor1.Value =p.pInfo.ToString();
        this.title.InnerText = p.pTitle.ToString();
        this.key.InnerText = p.pKey.ToString();
        this.des.InnerText = p.pDescription.ToString();
        this.down.InnerText = p.pButtom.ToString();
        this.content.Value = p.pSourse;
        this.HiddenId.Value = pid.ToString();
        this.proType.Value = p.Productmodel.ToString();
        Session["FCKeditor:UserFilesPath"] = "~/UploadFiles/productUpload/" + p.Productmodel.ToString();
  
    }
    #endregion

    #region AJAX修改
    public void EditPro()
    {
        p.pButtom = Request.Form["down"].ToString();
        p.pClassId = Convert.ToInt32(Request.Form["selectDp"].ToString());
        p.eDate = DateTime.Now;
        p.pDescription = Request.Form["des"].ToString();
        p.pInfo =Server.HtmlDecode(Request.Form["FCKeditor1"].ToString());
        p.pKey = Request.Form["key"].ToString();
        p.Price = 0;
        p.productName = Request["m_nameAjax"].ToString();
        p.pSort = Convert.ToInt32(Request.Form["psort"].ToString());
        p.ptag = Request.Form["keywordAjax"].ToString();
        p.pTitle = Request.Form["title"].ToString();
        p.Views = Convert.ToInt32(Request.Form["m_hitAjax"].ToString());
        p.pSourse = Request.Form["content"].ToString();
        p.productId = Convert.ToInt32(Request.Form["HiddenId"].ToString());
        p.Productmodel = Request.Form["proType"].ToString();
        if (pBll.Update(p))
        {
            Response.Write("1");
            Response.End();
        }

        
    }
    #endregion

    #region "抛弃服务器控件,自己绑定下拉框"
    public string ProductClassDp(string name)
    {
        try
        {
            StringBuilder str = new StringBuilder();
            str.Append("<select name='selectDp' id='" + name + "' style='width:192px;' runat='server'>");
            str.Append("<option value='0'>---顶级分类---</option>");
            str.Append(AddItems(0, 0, new StringBuilder(), ViewState["cid"].ToString()));
            str.Append("</select>");
            return str.ToString();
        }
        catch
        {
            return "";
        }
    }

    public string AddItems(int ParentID, int length, StringBuilder sb,string pclassid)
    {
        DataView dvTree = pcBll.GetDataView(ViewState["dt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            if (row["pClassId"].ToString() == pclassid)
            {
                sb.AppendFormat("<option value='" + row["pClassId"].ToString() + "' selected='selected'>{0}</option>", str + "|--" + row["pClassName"].ToString());
            }
            else
            sb.AppendFormat("<option value='" + row["pClassId"].ToString() + "'>{0}</option>", str + "|--" + row["pClassName"].ToString());
            AddItems(Convert.ToInt32(row["pClassId"].ToString()), length, sb,pclassid);
        }
        return sb.ToString();
    }
    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {

                mark += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        return mark;
    }
    #endregion
}
